<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id="app">
    <h2>总价格:{{totalPrice}}</h2>
    <h2>总价格:{{totalPrice2}}</h2>
    <h2>总价格:{{totalPrice3}}</h2>
    <h2>总价格:{{totalPrice4}}</h2>
  </div>

  <script>
    var vm = new Vue({
      el: '#app',
      data: {
        books: [
          { id: 110, name: 'Unix编程艺术', price: 110 },
          { id: 111, name: '代码大全', price: 100 },
          { id: 112, name: '深入理解计算机原理', price: 65 },
          { id: 113, name: '现代操作系统', price: 40 },
        ],
      },
      computed: {
        totalPrice: function () {
          return this.books.reduce(function (total, currentValue) {
            return total += currentValue.price
          }, 0)
        },
        totalPrice2: function () {
          let result = 0
          for (let i = 0; i < this.books.length; i++) {
            result += this.books[i].price
          }
          return result
        },
        totalPrice3: function () {
          let result3 = 0
          for (let i in this.books) {
            result3 += this.books[i].price
          }
          return result3
        },
        totalPrice4: function () {
          let result4 = 0
          for (let i of this.books) {
            console.log(i);
            result4 += i.price
          }
          return result4
        }
      },
      methods: {},
    });
  </script>
</body>

</html>